package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.UserCollection;
import com.powernode.mapper.UserCollectionMapper;
import com.powernode.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService{

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Override
    public void cancelOrAddCollection(String userId, Long prodId) {
        // 1. 按照条件查询数目如果有则取消，没有则添加
        Integer count = userCollectionMapper.selectCount(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId)
                .eq(UserCollection::getProdId, prodId)
        );

        if (count >= 1) {
            userCollectionMapper.delete(new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getUserId, userId)
                    .eq(UserCollection::getProdId, prodId));
        } else {
            UserCollection userCollection = new UserCollection();
            userCollection.setCreateTime(new Date());
            userCollection.setUserId(userId);
            userCollection.setProdId(prodId);
            userCollectionMapper.insert(userCollection);
        }
    }
}
